<?php


namespace App\Http\observer;


use App\Models\Banner;
use App\Models\Image as ImageModel;
use Illuminate\Support\Facades\Cache;

class BannersObserver
{
    public function updating(Banner $banner)
    {
        $old_banner=Banner::where("id",$banner["id"])->first();
        if($old_banner){
            Cache::put("banner_image",$old_banner["image"]);
        }
    }

    public function updated(Banner $banner)
    {

        $url=Cache::get("banner_image");
        Cache::forget("banner_image");
        if ($url&&$banner->image!=$url){
            $image=ImageModel::where("url",$url)->first();
            if ($image){
                ImageModel::find($image["id"])->delete();
            }
        }
    }

    public function deleted(Banner $banner)
    {
        $image=ImageModel::where("url",$banner->image)->first();
        if ($image){
            ImageModel::find($image["id"])->delete();
        }
    }
}
